#!/bin/bash

usage() {
  echo >&2
  echo >&2 "  Launches a VM with pre-baked settings. See http://bit.ly/XJnp5o for details."
  echo >&2
  echo >&2 "  Usage:"
  echo >&2 "    ./start-vm [--preallocate]"
  echo >&2
  echo >&2 "  Options:"
  echo >&2 "    --preallocate     Preallocates metadata for better disk performance"
  echo >&2 "                      (.qcow2 only)"
  echo >&2
  echo >&2 "  Environment variables (override if desired):"
  echo >&2 "    DISK={First .qcow2 file in current directory}"
  echo >&2 "    MAC_ADDR=DE:AD:BE:EF:30:22"
  echo >&2 "    RAM=2G"
  echo >&2 "    VNC_PORT=:10"
  echo >&2
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
  usage
  exit 1
fi

DISK="${DISK:-`ls -1 *.preallocated.qcow2 2>/dev/null | head -1`}"
DISK="${DISK:-`ls -1              *.qcow2 2>/dev/null | head -1`}"
MAC_ADDR=${MAC_ADDR:-DE:AD:BE:EF:30:22}
RAM=${RAM:-2G}
VNC_PORT=${VNC_PORT:-:10}

if [ "$DISK" = "" ]; then
  echo >&2
  echo >&2 "** DISK: Can't find any *.qcow2 files!"
  echo >&2
  exit 1
fi

if [ "$1" = "--preallocate" ]; then
  if [[ "$DISK" =~ \.preallocated\.qcow2$ ]]; then
    echo >&2 "** Disk appears to be already preallocated, aborting."
    exit 1
  else
    NEW_DISK="${DISK%.*}.preallocated.qcow2"
    echo "** Writing to $NEW_DISK..."
    qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata "$DISK" "$NEW_DISK"
    exit 0
  fi
fi

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root"
  exit 1
fi

echo "** Using the following settings:"
echo "  DISK=$DISK"
echo "  MAC_ADDR=$MAC_ADDR"
echo "  RAM=$RAM"
echo "  VNC_PORT=$VNC_PORT"
echo

cmd="qemu-kvm -cpu host -m $RAM -daemonize -vnc $VNC_PORT -vga vmware \
              -net nic,model=virtio,macaddr=$MAC_ADDR -net tap,script=qemu-ifup \
              -drive file=$DISK,cache=none,if=virtio"

echo "** Running the following command:"
echo $cmd
echo
echo "** Output:"

$cmd
